﻿using System;
using System.Linq;
using System.Linq.Expressions;
using Tss.MagicWeb.Domain.Abstract;
using Tss.MagicWeb.Domain.Entities;

namespace Tss.MagicWeb.Domain.Concrete
{
    public class DeckRepository : IDeckRepository, IDisposable
    {
        private MagicWebInterfaceEntities context = new MagicWebInterfaceEntities();

        public Deck Read(long id)
        {
            return context.Decks.FirstOrDefault(d => d.Id == id);
        }

        public IQueryable<Deck> Query()
        {
            return context.Decks;
        }

        public IQueryable<Deck> Query(Expression<Func<Deck, bool>> filter)
        {
            return context.Decks.Where(filter);
        }

        public void Create(Deck entity)
        {
            context.AddToDecks(entity);
            context.SaveChanges();
        }

        public void Delete(Deck entity)
        {
            context.Decks.DeleteObject(entity);
            context.SaveChanges();
        }

        public void Update(Deck entity)
        {
            context.SaveChanges();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool cleanManaged)
        {
            if (cleanManaged)
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
    }
}
